package com.gec.anan.payment.receiver;

import com.gec.anan.common.constant.MqConst;
import com.gec.anan.payment.service.WxPayService;
import com.gec.anan.payment.service.WxProfitsharingService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class PaymentReceiver {

    @Autowired
    private WxPayService wxPayService;
    @Autowired
    private WxProfitsharingService wxProfitsharingService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_PAY_SUCCESS, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ORDER),
            key = {MqConst.ROUTING_PAY_SUCCESS}
    ))
    public void paySuccess(String orderNo, Message message, Channel channel){
        log.info("支付成功队列{}", orderNo);
        wxPayService.handleOrder(orderNo);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //分账信息
//    @RabbitListener(queues = MqConst.QUEUE_PROFITSHARING)
//    public void handleProfitsharing(String param, Message message, Channel channel) throws IOException {
//        try {
//            log.info("分账信息{}",param);
//            ProfitsharingForm profitsharingForm = JSON.parseObject(param, ProfitsharingForm.class);
//            wxProfitsharingService.profitsharing(profitsharingForm);
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//        } catch (Exception e) {
//            log.info("分账信息报错{}", e.getMessage());
//            //任务执行失败，就退回队列继续执行，优化：设置退回次数，超过次数记录日志
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false, true);
//        }
//    }
//    public Boolean sendDealyMessage(String exchange, String routingKey, Object message, int delayTime) {
//        //1.创建自定义相关消息对象-包含业务数据本身，交换器名称，路由键，队列类型，延迟时间,重试次数
//        AnanCorrelationData correlationData = new AnanCorrelationData();
//        String uuid = "mq:" + UUID.randomUUID().toString().replaceAll("-", "");
//        correlationData.setId(uuid);
//        correlationData.setMessage(message);
//        correlationData.setExchange(exchange);
//        correlationData.setRoutingKey(routingKey);
//        correlationData.setDelay(true);
//        correlationData.setDelayTime(delayTime);
//        //2.将相关消息封装到发送消息方法中
//        redisTemplate.convertAndSend(exchange, routingKey, message, message1 -> {
//            message1.getMessageProperties().setDelay(delayTime * 1000);
//            return message1;
//        }, correlationData);
//
//        //3.将相关消息存入Redis  Key：UUID  相关消息对象  10 分钟
//        redisTemplate.opsForValue().set(uuid, JSONObject.toJSONString(correlationData), 10, TimeUnit.MINUTES);
//        return true;
//    }

}
